using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Rotempco.Core.Web.CRUD.Logic;
using Rotempco.Core.Web.CRUD.Objects;
using Rotempco.Core.Web.CRUD.Interfaces;
using Rotempco.Core.Web.Logic;
using Rotempco.Core.Web.CRUD.Objects.Enums;
using Rotempco.Core.Web.Objects.Enums;
using Rotempco.Core.ORM.Logic;

namespace Rotempco.Core.Web.CRUD.ACRUD
{
    public abstract class ACRUDRestore<TDomainObject, TPrimaryKey> : AAdminMaster
        where TDomainObject : class, new()
        where TPrimaryKey : struct
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
                (this.Master as ICRUDRestore).SetDefaultPageContainer(GetPageListContainer());

            string contractTemplateId = UrlLogic.GetIdFromUrlAsString();

            if (contractTemplateId != null)
                RestoreObject((TPrimaryKey)DBNullHelper.ToType(contractTemplateId, typeof(TPrimaryKey)));

            base.OnLoad(e);
        }

        public virtual void RestoreObject(TPrimaryKey domainObjectId)
        {
            ManagerInstanceHelper.GetManagerInstance<TDomainObject, TPrimaryKey>().Restore(domainObjectId);
            Response.Redirect(UrlLogic.GotoUrl(excludeUrlParameterEnums: new[] { UrlParameterEnum.Id }));
        }

        public abstract PageListContainer GetPageListContainer();
    }
}